﻿using System;
using System.Drawing;
using System.IO;
using System.Net;
using System.Text;

namespace HttpArchive
{
    public class Http
    {
        //公共Cookie
        private static CookieContainer Cookie = new CookieContainer();
        //浏览器信息
        private static string DefaultUserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36";
        /// <summary>
        /// Post提交
        /// </summary>
        /// <param name="postURL">目标页面网址</param>
        /// <param name="postData">需要提交的数据</param>
        /// <param name="pageEncoding">数据编码格式</param>
        /// <returns></returns>
        public static string POST(string postURL, string postData, Encoding pageEncoding)
        {
            HttpWebRequest webReq;
            HttpWebResponse webRes;

            byte[] bytesToPost = pageEncoding.GetBytes(postData);
            try
            {
                webReq = WebRequest.Create(postURL) as HttpWebRequest;
                webReq.Method = "POST";
                webReq.UserAgent = DefaultUserAgent;
                webReq.KeepAlive = true;
                webReq.ContentType = "application/x-www-form-urlencoded";
                webReq.CookieContainer = Cookie;            //设置上一个访问页面的cookie 保持session  
                webReq.ContentLength = bytesToPost.Length;  //Post数据长度

                Stream requestStream = webReq.GetRequestStream();
                requestStream.Write(bytesToPost, 0, bytesToPost.Length);    //写入Post数据  
                requestStream.Close();

                webRes = webReq.GetResponse() as HttpWebResponse;
                Cookie = webReq.CookieContainer;//访问后更新cookie  
                Stream responseStream = webRes.GetResponseStream();

                string resData;
                using (StreamReader resSR = new StreamReader(responseStream, pageEncoding))
                {
                    resData = resSR.ReadToEnd();
                    resSR.Close();
                    responseStream.Close();
                }
                return resData;
            }
            catch (Exception)
            {
                return null;
            }
        }
        /// <summary>
        /// Get数据
        /// </summary>
        /// <param name="getURL">目标页面网址</param>
        /// <param name="pageEncoding">数据编码格式</param>
        /// <returns></returns>
        public static string Get(string getURL, Encoding pageEncoding)
        {
            HttpWebRequest webReq;
            HttpWebResponse webRes;
            
            try
            {
                webReq = WebRequest.Create(getURL) as HttpWebRequest;
                webReq.Timeout = 2000;
                webReq.Method = "GET";
                webReq.UserAgent = DefaultUserAgent;
                webReq.KeepAlive = true;
                webReq.ContentType = "application/x-www-form-urlencoded";
                webReq.CookieContainer = Cookie;    //设置上一个访问页面的cookie 保持session

                webRes = webReq.GetResponse() as HttpWebResponse;
                Cookie = webReq.CookieContainer;//访问后更新cookie  
                Stream responseStream = webRes.GetResponseStream();

                string resData;
                using (StreamReader resSR = new StreamReader(responseStream, pageEncoding))
                {
                    resData = resSR.ReadToEnd();
                    resSR.Close();
                    responseStream.Close();
                }
                return resData;
            }
            catch (Exception)
            {
                return null;
            }
        }
        /// <summary>
        /// 图片获取
        /// </summary>
        /// <param name="getURL">目标图片网址</param>
        /// <returns>返回Image图片</returns>
        public static Image GetImg(string getURL)
        {
            HttpWebRequest webReq;
            HttpWebResponse webRes;

            try
            {
                webReq = WebRequest.Create(getURL) as HttpWebRequest;
                webReq.Timeout = 2000;
                webReq.Method = "GET";
                webReq.UserAgent = DefaultUserAgent;
                webReq.KeepAlive = true;
                webReq.ContentType = "application/x-www-form-urlencoded";
                webReq.CookieContainer = Cookie;    //设置上一个访问页面的cookie 保持session

                webRes = webReq.GetResponse() as HttpWebResponse;
                Cookie = webReq.CookieContainer;//访问后更新cookie  
                Stream responseStream = webRes.GetResponseStream();
                Bitmap sourcebm = new Bitmap(responseStream);
                return sourcebm;
            }
            catch (Exception)
            {
                return null;
            }
        }
        /// <summary>
        /// Unicode转汉字
        /// </summary>
        /// <param name="text">含有Unicode编码的字符串</param>
        /// <returns>转码后的字符串</returns>
        public static string UnicodeToGB(string text)
        {
            System.Text.RegularExpressions.MatchCollection mc = System.Text.RegularExpressions.Regex.Matches(text, "\\\\u([\\w]{4})");
            if (mc != null && mc.Count > 0)
            {
                foreach (System.Text.RegularExpressions.Match m2 in mc)
                {
                    string v = m2.Value;
                    string word = v.Substring(2);
                    byte[] codes = new byte[2];
                    int code = Convert.ToInt32(word.Substring(0, 2), 16);
                    int code2 = Convert.ToInt32(word.Substring(2), 16);
                    codes[0] = (byte)code2;
                    codes[1] = (byte)code;
                    text = text.Replace(v, Encoding.Unicode.GetString(codes));
                }
            }
            else
            {

            }
            return text;
        }
    }
}
